package cn.edu.zime.tjh.iotapp.chat;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ChatMessage {
    public static final int TYPE_SENT = 1;
    public static final int TYPE_RECEIVED = 2;
    
    private int type;
    private String message;
    private long timestamp;
    private String formattedTime;
    
    public ChatMessage(int type, String message) {
        this.type = type;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
        
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        this.formattedTime = sdf.format(new Date(timestamp));
    }
    
    // 用于从服务器响应创建消息
    public static ChatMessage fromJson(String message, boolean isReceived) {
        return new ChatMessage(
            isReceived ? TYPE_RECEIVED : TYPE_SENT, 
            message
        );
    }
    
    public int getType() {
        return type;
    }
    
    public String getMessage() {
        return message;
    }

    public String getFormattedTime() {
        return formattedTime;
    }
    
    // 添加方法用于更新消息内容
    public void updateMessage(String newMessage) {
        this.message = newMessage;
    }
} 